package com.m2bshop.shop.controller;

import com.alibaba.fastjson.JSONArray;
import com.m2bshop.shop.pojo.Result.Result;
import com.m2bshop.shop.pojo.Store;
import com.m2bshop.shop.pojo.User;
import com.m2bshop.shop.service.StoreService;
import com.m2bshop.shop.service.UserService;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/store")
public class StoreController {

    @Autowired
    private UserService userService;
    @Autowired
    private StoreService storeService;

    @RequestMapping("/getbvoinfo")
    public String selectBov(HttpSession session, Model model){
        System.out.println("role: bvo");
        String bvoName = session.getAttribute("username").toString();
        User bvo = userService.getUserByUsername(bvoName);
        System.out.println("controller:get user by name:"+bvo);
        model.addAttribute("id",bvo.getId());
        model.addAttribute("userName",bvo.getUsername());
        model.addAttribute("tel",bvo.getPhonenumber());
        return "bvoinfo";
    }

    @RequestMapping("/updatebvo")
    @ResponseBody
    public Result updateBvo(@RequestBody User user,HttpSession session){
        System.out.println(user);
        userService.updateUserByNameTel(user);
        session.setAttribute("username",user.getUsername());
        return Result.ok(user);
    }

    @RequestMapping("/go{name}")
    public String jumpHtml(@PathVariable String name, Model model,HttpSession session){
        String bvoId = session.getAttribute("username").toString();
        model.addAttribute("bvoId",bvoId);
        System.out.println(name);
        return name;
    }

    @RequestMapping("/getstoretype")
    @ResponseBody
    public Result findByType(@RequestBody Map map, Model model){
        String bvoId = map.get("bvoId").toString();
        String marketType = map.get("marketType").toString();
        List<Store> stores = storeService.findByBvoIdType(bvoId,marketType);
        System.out.println("bvoid:"+bvoId+"type:"+marketType);
        System.out.println("store:"+stores);
        JSONArray jsonArray = (JSONArray) JSONArray.toJSON(stores);
        System.out.println(jsonArray);
        return Result.ok(stores);
    }

    @RequestMapping("/findstore")
    @ResponseBody
    public Result findByStoreId(String storeId){
        Store store = storeService.findByStoreId(storeId);
        System.out.println(store);
        return Result.ok(store);
    }

    @RequestMapping("/editst")
    @ResponseBody
    public Result updateStore(@RequestBody Map map){
        System.out.println("update_____________"+map.get("pkStore").toString());
        Store store = new Store();
        store.setPkStore(Integer.parseInt(map.get("pkStore").toString()));
        store.setStoreId(map.get("storeId").toString());
        store.setStoreName(map.get("storeName").toString());
        store.setMarketType(map.get("marketType").toString());
        store.setMarketplaceId(map.get("marketplaceId").toString());
        store.setMwsToken(map.get("mwsToken").toString());
        store.setBvoId(map.get("bvoId").toString());
        storeService.updateStore(store);
        System.out.println("here");
        System.out.println(store);
        return Result.ok(store);
    }

    @RequestMapping("/addst")
    @ResponseBody
    public Result addStore(@RequestBody Map map){
        System.out.println("requestbody:"+map);
        Store store = new Store();
        store.setStoreId(map.get("storeId").toString());
        store.setStoreName(map.get("storeName").toString());
        store.setBvoId(map.get("bvoId").toString());
        store.setMarketplaceId(map.get("marketplaceId").toString());
        store.setMarketType(map.get("marketType").toString());
        store.setMwsToken(map.get("mwsToken").toString());
        System.out.println("store:"+store);
        storeService.addStore(store);
        return Result.ok(store);
    }

    @RequestMapping("/delst")
    @ResponseBody
    public Result deleteStore(@RequestBody Map map){
        System.out.println("map:"+map);
        storeService.updateStoreState(Integer.parseInt(map.get("pkStore").toString()), map.get("state").toString());
        return Result.ok();
    }

}
